;-------------------------------------------------------------LICENSE--------------------------------------------------------------;
;                                                                                                                                  ;
;The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    ;
;and Message Passing Interface (MPI) parallelization.                                                                              ;
;                                                                                                                                  ;
;Copyright (C) 2012                                                                                                                ;
;Ronglin Jiang                                                                                                                     ;
;rljiang@ssc.net.cn                                                                                                                ;
;585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             ;
;                                                                                                                                  ;
;This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         ;
;as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             ;
;                                                                                                                                  ;
;This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    ;
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        ;
;                                                                                                                                  ;
;You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     ;
;Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   ;
;                                                                                                                                  ;
;-------------------------------------------------------------LICENSE--------------------------------------------------------------;

pro usr_plot_sys, entry_dev, IMAGE_NAME = image_name, START = start, DONE = done,  $
  PS = ps, XSIZE = xsize, YSIZE = ysize, PIXMAP = pixmap, CMYK = cmyk

if not keyword_set(done) then entry_dev = !d.name

if not keyword_set(cmyk) then cmyk = 0

if n_elements(ps) eq 0 then begin
  plot_dev=!d.name
endif else begin 
  if ps eq 0 then plot_dev=!d.name
  if ps eq 1 then plot_dev='ps'
endelse

if plot_dev ne 'ps' then begin
  !usr_scale=1
  if n_elements(start) eq 0 and n_elements(done) eq 0 then begin
    print,' Plot start or done?'
    retall
  endif
  
  if n_elements(start) eq 1 then begin
    if start eq 1 then done=0
    if start eq 0 then done=1
  endif else begin
    start=0
    done=1
  endelse

  if start eq 1 then begin
    device,get_screen_size=screen_size
    if n_elements(xsize) eq 0 then xsize=600
    if n_elements(ysize) eq 0 then ysize=600
    if n_elements(pixmap) eq 0 then pixmap=0
    window,xsize=xsize,ysize=ysize,pixmap=pixmap,/free
  endif

  if done eq 1 then begin
    if n_elements(image_name) eq 0 then image_name='idl'
    image=tvrd(true=1)
    write_png,image_name+'.png',image
    wdelete,!d.window
  endif
endif

if plot_dev eq 'ps' then begin
  set_plot,'ps'
  !usr_scale=27.5
  if n_elements(start) eq 0 and n_elements(done) eq 0 then begin
    print,' Plot start or done?'
    retall
  endif
  
  if n_elements(start) eq 1 then begin
    if start eq 1 then done=0
    if start eq 0 then done=1
  endif else begin
    start=0
    done=1
  endelse

  if start eq 1 then begin
    if n_elements(xsize) eq 0 then xsize=600/!usr_scale
    if n_elements(ysize) eq 0 then ysize=600/!usr_scale
    if n_elements(image_name) eq 0 then image_name='idl'
    device,filename=image_name+'.eps',/color,bits_per_pixel=8,encapsulated=1,glyph_cache=1024, $
      xsize=xsize/!usr_scale,ysize=ysize/!usr_scale,cmyk=cmyk
    if strlowcase(!version.os_family) eq 'unix' then begin
      defsysv,'!usr_thick',0.012*!usr_win_size
      defsysv,'!usr_xthick',0.008*!usr_win_size
      defsysv,'!usr_ythick',0.008*!usr_win_size
      defsysv,'!usr_othick',0.008*!usr_win_size
    endif
  endif
  if done eq 1 then begin
    device, /close_file
  endif
endif

if keyword_set(done) then set_plot, entry_dev

end
